<template>
  <div style="padding-bottom:120px;">
    <div class="search_wrap">
      <van-search shape="round">
        <template #left>
          <div class="back">
            <van-icon name="arrow-left"></van-icon>
          </div>
        </template>
      </van-search>
      <div class="category_wrap">
        <div class="category_left">
          <van-sidebar v-model="activeKey">
            <van-sidebar-item :title="item.category_cn"
                              v-for="(item,index) in oneCategory"
                              :key="index"
                              @click="loadTwoCategory(item.category_id)" />

          </van-sidebar>
        </div>
        <div class="category_right">
          <div class="category_list">
            <div class="list_one"
                 v-for="(item,index) in twoCategory"
                 :key="index"
                 @click="turnSearchList(item.category_id)">
              <img :src="item.img">
              <p>{{item.category_cn}}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getCategoryApi } from '../../api/category'
export default {
  data () {
    return {
      activeKey: 0,
      oneCategory: [],
      twoCategory: []
    }
  },
  methods: {
    loadTwoCategory (cid) { //加载二级分类
      getCategoryApi(cid).then(res => {
        console.log(res)
        this.twoCategory = res.data;
      })
    },
    turnSearchList (cid) { //跳转搜索列表页 并传参
      this.$router.push({ name: "searchList", params: { cid: cid } })
    }
  },
  created () {
    // 获取一级分类
    getCategoryApi(0).then(res => {
      console.log(res)
      this.oneCategory = res.data;
      this.loadTwoCategory(res.data[0].category_id)//默认加载第一项的二级分类数据
    })

  }
}
</script>
<style lang="less" scoped>
.back {
  font-size: 50px;
  background: #f7f7f7;
  margin-right: 10px;
}
.category_wrap {
  width: 100%;
  display: flex;
  height: 70vh;
  .category_left {
    width: 30%;
    height: 80vh;
  }
  .category_right {
    width: 70%;
    overflow: auto;
    height: 90vh;
    .category_list {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      background: white;
      justify-content: space-around;
      .list_one {
        width: 40%;
        text-align: center;
        img {
          width: 100%;
          height: 240px;
        }
      }
    }
  }
}
</style>